package com.seeyon.apps.cxgd.initializer;

import com.seeyon.apps.cxgd.kit.DBKit;
import com.seeyon.ctp.common.AbstractSystemInitializer;
import com.seeyon.ctp.common.log.CtpLogFactory;
import org.apache.commons.logging.Log;

import java.util.List;
import java.util.Map;

public class SyncPluginInitializer extends AbstractSystemInitializer {
    private static final Log log = CtpLogFactory.getLog(SyncPluginInitializer.class);


    public void initialize() {
        try {
            // 构建查询特定表是否存在的SQL语句
            StringBuilder stbSql = new StringBuilder();
            stbSql.append("SELECT NAME FROM SYSOBJECTS WHERE NAME = 'PLUGIN_TEMP_DATA'");
            // 执行SQL查询，并获取结果集
            List<Map<String, Object>> dataList = DBKit.excuteSQL(stbSql.toString(), null);
            // 如果查询结果为空，即表不存在，则创建该表
            if (dataList.isEmpty()) {
                // 构建创建表的SQL语句
                StringBuilder stb = new StringBuilder();
                stb.append("CREATE TABLE PLUGIN_TEMP_DATA (").append("\r\n");
                stb.append("ID bigint NOT NULL,").append("\r\n");
                stb.append("FORM_ID bigint,").append("\r\n");
                stb.append("WL_CODE varchar(255) COLLATE Chinese_PRC_90_CI_AI  NULL,").append("\r\n");
                stb.append("CONSTRAINT PLUGIN_SYNC_DATA_ID PRIMARY KEY CLUSTERED (ID))").append("\r\n");
                // 记录创建表的SQL语句
                log.info("创建数据库表sql语句:" + stb.toString());
                // 执行创建表操作，并记录影响的行数
                int num = DBKit.createTable(stb.toString());
                log.info("sql语句执行影响条数:" + num);
            }

        } catch (Exception e) {
            // 记录创建表失败的错误
            log.error("创建数据库表失败:", e);
        }
    }


}
